package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.knowledge.domain.SysCalendarSharing;
import cn.cdeden.knowledge.domain.dto.SysCalendarSharingDto;
import cn.cdeden.knowledge.domain.dto.SysCalendarSharingQueryParam;
import cn.cdeden.knowledge.mapper.SysCalendarSharingMapper;
import cn.cdeden.knowledge.service.SysCalendarSharingService;
import cn.cdeden.knowledge.utils.PageUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author Guojunxu
* @date 2024-04-17
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = SysCalendarSharingService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysCalendarSharingServiceImpl extends ServiceImpl<SysCalendarSharingMapper, SysCalendarSharing> implements SysCalendarSharingService {

    // private final RedisUtils redisUtils;
    private final SysCalendarSharingMapper sysCalendarSharingMapper;

    @Override
    public TableDataInfo<SysCalendarSharingDto> queryAll(SysCalendarSharingQueryParam query, Pageable pageable) {
        IPage<SysCalendarSharing> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<SysCalendarSharing> page = sysCalendarSharingMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        TableDataInfo<SysCalendarSharing> build = TableDataInfo.build(page);
        return MapstructUtils.convertTableDataInfo(build, SysCalendarSharingDto.class);
    }

    @Override
    public List<SysCalendarSharingDto> queryAll(SysCalendarSharingQueryParam query){
        return MapstructUtils.convert(sysCalendarSharingMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), SysCalendarSharingDto.class);
    }

    @Override
    public SysCalendarSharing getById(Integer id) {
        return sysCalendarSharingMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public SysCalendarSharingDto findById(Integer id) {
        return MapstructUtils.convert(getById(id), SysCalendarSharingDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(SysCalendarSharingDto resources) {
        SysCalendarSharing entity = MapstructUtils.convert(resources, SysCalendarSharing.class);
        return this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(SysCalendarSharingDto resources){
        SysCalendarSharing entity = MapstructUtils.convert(resources, SysCalendarSharing.class);
        boolean ret = this.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(Set<Integer> ids){
        // delCaches(ids);
        return this.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.deleteByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<SysCalendarSharingDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (SysCalendarSharingDto sysCalendarSharing : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("日历共享人员ID", sysCalendarSharing.getCalendarSharingId());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
